package com.liukai.todo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liukai.todo.entity.RespBean;
import com.liukai.todo.entity.TodoInfo;
import com.liukai.todo.mapper.TodoMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liukai.todo.service.ITodoService;
import com.sun.tools.javac.comp.Todo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liukai
 * @since 2021-07-30
 */
@Service
public class TodoServiceImpl extends ServiceImpl<TodoMapper, TodoInfo> implements ITodoService {

    @Autowired
    private TodoMapper todoMapper;


    /**
     * 新增一条待办事项
     *
     * @param todoInfo
     * @return
     */
    @Override
    public RespBean addTodo(TodoInfo todoInfo) {

        int insert = todoMapper.insert(todoInfo);
        if (insert > 0) {
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    /**
     * 获取所有的待办事项
     *
     * @return
     */
    @Override
    public RespBean getAllTodo() {
        List<TodoInfo> todoInfos = todoMapper.selectList(null);
        return RespBean.success("find success", todoInfos);
    }

    /**
     * 根据userId获取该用户的所有待办事项
     *
     * @param userId
     * @return
     */
    @Override
    public RespBean getTodoByUserId(Integer userId) {
        QueryWrapper<TodoInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId);
        List<TodoInfo> todoInfos = todoMapper.selectList(wrapper);
        return RespBean.success("find success", todoInfos);
    }

    /**
     * 根据userName获取该用户的所有待办事项
     *
     * @param userName
     * @return
     */
    @Override
    public RespBean getTodoByUserName(String userName) {
        if (("").equals(userName)) {
            return RespBean.error("参数不能为空");
        }
        List<TodoInfo> todoInfos = todoMapper
                .selectList(new QueryWrapper<TodoInfo>()
                        .eq("userName", userName));
        return RespBean.success("查找成功", todoInfos);
    }

    /**
     * 根据userId和todoName获取该用户的名字为todoName的待办事项
     *
     * @param todoName
     * @return
     */
    @Override
    public RespBean getTodoByTodoName(String todoName, Integer userId) {
        if (("").equals(todoName) || userId == null) {
            return RespBean.error("参数不能为空");
        }
        List<TodoInfo> todoInfos = todoMapper
                .selectList(new QueryWrapper<TodoInfo>()
                        .eq("todoName", todoName)
                        .eq("userId", userId));
        return RespBean.success("查找成功", todoInfos);
    }

    /**
     * 根据todoId获取待办事项
     *
     * @param todoId
     * @return
     */
    @Override
    public RespBean getTodoByTodoId(Integer todoId) {
        if (todoId == null) {
            return RespBean.error("参数不能为空");
        }
        TodoInfo todoInfo = todoMapper
                .selectOne(new QueryWrapper<TodoInfo>()
                        .eq("todoId", todoId));
        return RespBean.success("查找成功", todoInfo);
    }

    /**
     * 根据todoId修改待办事项
     *
     * @param todoInfo
     * @return
     */
    @Override
    public RespBean updateTodoByTodoId(TodoInfo todoInfo) {
        if (todoInfo == null) {
            return RespBean.error("参数不能为空");
        }
        int i = todoMapper.updateById(todoInfo);
        if (i >= 0) {
            return RespBean.success("修改成功");
        }
        return RespBean.error("修改失败");
    }

    /**
     * 根据userId删除待办事项
     *
     * @param todoId
     * @return
     */
    @Override
    public RespBean deleteTodoByTodoId(Integer todoId) {
        if (todoId == null) {
            return RespBean.error("参数不能为空");
        }
        int i = todoMapper.deleteById(todoId);
        if (i >= 0) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

}
